using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ApplicationLibrary.Preferences;
using ApplicationLibrary.SqlConnector;

namespace ApplicationLibrary
{
    public static class ExtendEnum
    {
        public static int Int(this Enum enums)
        {
            return (int)Convert.ChangeType(enums, typeof(int));
        }
    }

    public static class ExtendDate
    {
        public static DateTime StartYear(this DateTime date)
        {
            return new DateTime(date.Year, 1, 1);
        }

        public static DateTime EndYear(this DateTime date)
        {
            return new DateTime(date.Year, 12, 31);
        }

        public static DateTime EmptyDate(this DateTime date)
        {
            return new DateTime(1799, 12, 31);
        }

        public static bool IsEmpty(this DateTime date)
        {
            return date == EmptyDate(date);
        }
    }

    public static class ExtendedString
    {
        public static string CommaConcat(this string strings, string value)
        {
            return strings.Length == 0
                ? value
                : string.Concat(strings, ",", value);
        }

        public static string SeparConcat(this string strings, string value, string separ)
        {
            return strings.Length == 0
                ? value
                : string.Concat(strings, separ, value);
        }

        public static bool IsEmpty(this string strings)
        {
            return string.IsNullOrEmpty(strings);
        }
    }

    public static class GlobalInfo
    {
        public static DateTime CurrentDate;

        public static Form mainForm { get; set; }

        public static Dictionary<int, string> CounterTypes = new Dictionary<int, string>();
        public static Dictionary<string, string> CodeTypes = new Dictionary<string, string>();

        public static UserInfo UserInfo = new UserInfo();
        public static DBaseInfo DBaseInfo = new DBaseInfo();
        public static StripInfo ToolStrip;
        public static ComputerInfo ComputerInfo = new ComputerInfo();
        public static GlobalPref globalPref;
        public static string[] UsersLogged;

        public static SqlABConnection SqlConnection
        {
            get
            {
                System.Diagnostics.Debug.Assert(DBaseInfo.dbManager != null);
                System.Diagnostics.Debug.Assert(DBaseInfo.dbManager.DB_Connection != null);

                return DBaseInfo.dbManager.DB_Connection;
            }
        }

        public delegate Form GlobalInfoFormEventHandler(object sender, GlobalInfoFormArgs pe);

        public static event GlobalInfoFormEventHandler OpenNewForm;

        public static event GlobalInfoFormEventHandler OpenNewDataManager;

        public static event GlobalInfoFormEventHandler CreateNewForm;

        public static Form MainForm { get { return mainForm; } }

        public static Form OpenDataManager(string nameSpace)
        {
            return OpenDataManager(nameSpace, false);
        }

        public static Form OpenDataManager(string nameSpace, bool showdialog)
        {
            Form form = null;
            if (OpenNewDataManager != null)
                form = OpenNewDataManager(null, new GlobalInfoFormArgs(nameSpace, false, showdialog));

            return form;
        }

        public static Form OpenForm(string nameSpace, bool modal)
        {
            Form form = null;
            if (OpenNewForm != null)
                form = OpenNewForm(null, new GlobalInfoFormArgs(nameSpace, modal, true));

            return form;
        }

        public static Form CreateForm(string nameSpace)
        {
            Form form = null;
            if (CreateNewForm != null)
                form = CreateNewForm(null, new GlobalInfoFormArgs(nameSpace, false, true));

            return form;
        }

        public static void AddCounter(int key, string value)
        {
            System.Diagnostics.Debug.Assert(!CounterTypes.ContainsKey(key), "Counter duplicated");
            CounterTypes.Add(key, value);
        }

        public static void AddCodeType<T>(string key, T value)
        {
            value.ToString();
        }

        public static void AddCodeType(string key, string value)
        {
            System.Diagnostics.Debug.Assert(key.Length <= 8, "Counterkey too long");
            System.Diagnostics.Debug.Assert(!CodeTypes.ContainsKey(key), "Counter duplicated");
            CodeTypes.Add(key, value);
        }

        public static int GetCounter(string value)
        {
            System.Diagnostics.Debug.Assert(CounterTypes.ContainsValue(value), "Unknown counter");

            foreach (KeyValuePair<int, string> kvp in CounterTypes)
                if (kvp.Value == value)
                    return kvp.Key;
            return -1;
        }

        public static string GetCodeType(string value)
        {
            System.Diagnostics.Debug.Assert(CodeTypes.ContainsValue(value), "Unknown codetype");

            foreach (KeyValuePair<string, string> kvp in CodeTypes)
                if (kvp.Value == value)
                    return kvp.Key;
            return null;
        }
    }

    public class GlobalInfoFormArgs : EventArgs
    {
        private NameSpace nameSpace;
        private bool modal;
        private bool showDialog;

        public NameSpace Namespace { get { return nameSpace; } }

        public bool Modal { get { return modal; } }

        public bool ShowDialog { get { return showDialog; } }

        public GlobalInfoFormArgs(string nameSpace, bool modal, bool showdialog)
        {
            this.nameSpace = new NameSpace(nameSpace);
            this.modal = modal;
            this.showDialog = showdialog;
        }
    }

    public class UserInfo
    {
        public string User = string.Empty;
        public UserType userType = UserType.Administrator;
    }

    public class DBaseInfo
    {
        public SqlManager dbManager;
        public UserType userType = UserType.Administrator;
    }

    public class PrintInfo
    {
        public string ReportName { get; set; }

        public PrintType PrintType { get; set; }

        public PrintInfo(string reportName, PrintType printType)
        {
            ReportName = reportName;
            PrintType = printType;
        }
    }

    public class ComputerInfo
    {
        private System.Drawing.Printing.PrinterSettings oPS = new System.Drawing.Printing.PrinterSettings();

        public string[] Printers;

        public ComputerInfo()
        {
            Printers = new string[System.Drawing.Printing.PrinterSettings.InstalledPrinters.Count];
            System.Drawing.Printing.PrinterSettings.InstalledPrinters.CopyTo(Printers, 0);
        }

        public string ComputerName
        {
            get
            {
                return System.Environment.MachineName;
            }
        }

        public string UserName
        {
            get
            {
                return System.Environment.UserName;
            }
        }

        public string DefaultPrinter
        {
            get
            {
                return oPS.PrinterName;
            }
        }

        public short DefaultCopies
        {
            get
            {
                return oPS.Copies;
            }
        }
    }

    public class StripInfo
    {
        private System.Windows.Forms.ToolStripStatusLabel Label = null;
        private System.Windows.Forms.ToolStripProgressBar ProgBar = null;

        public StripInfo(ToolStripStatusLabel Label, ToolStripProgressBar ProgBar)
        {
            this.Label = Label;
            this.ProgBar = ProgBar;
        }

        public string Text { set { Label.Text = value; Application.DoEvents(); } }
    }

    public class NameSpace
    {
        public string Folder { get; private set; }

        public string Library { get; private set; }

        public string Module { get; private set; }

        public string Application { get; set; }

        public NameSpace(string Folder, string Library)
        {
            this.Folder = Folder;
            this.Library = Library;
            this.Application = "";
            this.Module = "";
        }

        public NameSpace(string Folder, string Library, string Module)
        {
            this.Folder = Folder;
            this.Library = Library;
            this.Module = Module;
        }

        public NameSpace(string Folder, string Library, string Module, string Application)
        {
            this.Folder = Folder;
            this.Library = Library;
            this.Module = Module;
            this.Application = Application;
        }

        public NameSpace(string ns)
        {
            string[] splitted = ns.Split(new char[] { '.' });

            if (splitted.Length > 0) Folder = splitted[0];
            if (splitted.Length > 1) Library = splitted[1];
            if (splitted.Length > 2) Module = splitted[2];
            if (splitted.Length > 3) Application += string.Concat(splitted[3]);
            if (splitted.Length > 4) Application += string.Concat(".", splitted[4]);
            if (splitted.Length > 5) Application += string.Concat(".", splitted[5]);
        }

        public string ModulePath
        {
            get { return string.Concat(Folder, ".", Library, ".", Module); }
        }

        public string ApplicationPath
        {
            get
            {
                string val = string.Empty;
                if (Library.Length != 0)
                    val = string.Concat(Library, ".");
                if (Module.Length != 0)
                    val = string.Concat(val, Module, ".");
                if (Application.Length != 0)
                    val = string.Concat(val, Application);
                return val;
            }
        }

        public override string ToString()
        {
            string ns = string.Concat(Library, ".", Module);
            if (Application.Length != 0)
                ns = string.Concat(ns, ".", Application);
            return ns;
        }
    }

    #region Menu Collection

    public class ApplicationMenuModule : Object
    {
        public List<ApplicationMenuFolder> MenuFolders;

        public string Menu { get; private set; }

        public string Image { get; private set; }

        public NameSpace Namespace { get; private set; }

        public ApplicationMenuModule(NameSpace nameSpace, string menu, string image)
        {
            MenuFolders = new List<ApplicationMenuFolder>(1);
            this.Namespace = nameSpace;
            this.Menu = menu;
            this.Image = image;
        }
    }

    public class ApplicationMenuFolder : Object
    {
        public List<ApplicationMenuItem> MenuItems;

        public string Folder { get; private set; }

        public ApplicationMenuFolder(string folder)
        {
            this.Folder = folder;
            MenuItems = new List<ApplicationMenuItem>(1);
        }
    }

    public class ApplicationMenuItem : Object
    {
        public string Title { get; private set; }

        public NameSpace Namespace { get; private set; }

        public FormType DocumentType { get; private set; }

        public UserType UserGrant { get; private set; }

        public string Icon { get; private set; }

        public ApplicationMenuItem(string Title, NameSpace Namespace, FormType documentType, UserType userType)
        {
            this.Title = Title;
            this.Namespace = Namespace;
            this.DocumentType = documentType;
            this.UserGrant = userType;
        }
    }

    #endregion
}